<template>
  <div class="app-container">
    <div class="main-container">
      <!-- 页面标题 -->
      <div class="page-header">
          <router-link to="/main/biao/wailai" class="back-btn" style="white-space: nowrap;">
            <i class="el-icon-arrow-left"></i> 返回</router-link>
        <div class="header-title">
          <i class="el-icon-document"></i>
          <span>文件发放与回收记录列表</span>
        </div>
      </div>

      <!-- 搜索区域 -->
      <el-card class="search-card" shadow="hover">
        <el-form :inline="true" :model="listQuery" class="search-form">
          <el-form-item>
            <el-input
              v-model="listQuery.keyword"
              placeholder="请输入文件编号/名称/类别"
              clearable
              @keyup.enter.native="handleFilter"
            >
              <el-button slot="append" icon="el-icon-search" @click="handleFilter" />
            </el-input>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" icon="el-icon-plus" @click="handleCreate">
              新增记录
            </el-button>
          </el-form-item>
        </el-form>
      </el-card>

      <!-- 表格区域 -->
      <el-card class="table-card" shadow="hover">
        <el-table
          v-loading="loading"
          :data="list"
          border
          fit
          highlight-current-row
          style="width: 100%"
        >
          <el-table-column type="index" label="序号" width="60" align="center" />
          <el-table-column prop="fileNumber" label="文件编号" min-width="120" show-overflow-tooltip>
            <template slot-scope="{row}">
              <span class="cell-text">{{ row.fileNumber }}</span>
            </template>
          </el-table-column>
          <el-table-column prop="fileName" label="文件名称" min-width="180" show-overflow-tooltip>
            <template slot-scope="{row}">
              <span class="cell-text">{{ row.fileName }}</span>
            </template>
          </el-table-column>
          <el-table-column prop="createTime" label="创建时间" width="180" align="center">
            <template slot-scope="{row}">
              <span>{{ row.createTime }}</span>
            </template>
          </el-table-column>
          <el-table-column prop="updateTime" label="更新时间" width="180" align="center">
            <template slot-scope="{row}">
              <span>{{ row.updateTime }}</span>
            </template>
          </el-table-column>
          <el-table-column label="操作" width="300" align="center" fixed="right">
            <template slot-scope="{row}">
              <el-button
                type="primary"
                size="mini"
                icon="el-icon-view"
                @click="handleView(row)"
              >
                查看
              </el-button>
              <el-button
                type="success"
                size="mini"
                icon="el-icon-edit"
                @click="handleUpdate(row)"
              >
                编辑
              </el-button>
              <el-button
                type="danger"
                size="mini"
                icon="el-icon-delete"
                @click="handleDelete(row)"
              >
                删除
              </el-button>
            </template>
          </el-table-column>
        </el-table>

        <!-- 分页 -->
        <div class="pagination-container">
          <el-pagination
            :current-page="listQuery.page"
            :page-sizes="[10, 20, 30, 50]"
            :page-size="listQuery.limit"
            :total="total"
            background
            layout="total, sizes, prev, pager, next, jumper"
            @size-change="handleSizeChange"
            @current-change="handleCurrentChange"
          />
        </div>
      </el-card>
    </div>
  </div>
</template>

<script>
import { getFileDistributionList, deleteFileDistribution } from '@/api/fileDistribution'

export default {
  name: 'FfhuishouList',
  data() {
    return {
      list: [],
      total: 0,
      loading: false,
      listQuery: {
        page: 1,
        limit: 10,
        keyword: ''
      }
    }
  },
  created() {
    this.getList()
  },
  methods: {
    async getList() {
      this.loading = true
      try {
        const response = await getFileDistributionList(this.listQuery)
        this.list = response.data.records
        this.total = response.data.total
      } catch (error) {
        console.error('获取列表失败:', error)
        this.$message.error('获取列表失败')
      }
      this.loading = false
    },
    handleFilter() {
      this.listQuery.page = 1
      this.getList()
    },
    handleSizeChange(val) {
      this.listQuery.limit = val
      this.getList()
    },
    handleCurrentChange(val) {
      this.listQuery.page = val
      this.getList()
    },
    handleCreate() {
      this.$router.push('/main/biao/ffhuishou')
    },
    handleView(row) {
      this.$router.push({
        path: '/main/biao/ffhuishou',
        query: {
          id: row.id,
          type: 'view'
        }
      })
    },
    handleUpdate(row) {
      this.$router.push({
        path: '/main/biao/ffhuishou',
        query: {
          id: row.id,
          type: 'edit'
        }
      })
    },
    handleDelete(row) {
      this.$confirm('确认删除该记录吗？', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async () => {
        try {
          await deleteFileDistribution(row.id)
          this.$message.success('删除成功')
          this.getList()
        } catch (error) {
          console.error('删除失败:', error)
          this.$message.error('删除失败')
        }
      }).catch(() => {})
    }
  }
}
</script>

<style lang="scss" scoped>
.app-container {
  padding: 24px;
  background-color: antiquewhite;
  min-height: calc(100vh - 48px);
  width: 1200px;
  margin: 0 auto;
}

.main-container {
  max-width: 1800px;
  margin: 0 auto;
}

.page-header {
  background-color: #fff;
  border-radius: 8px;
  box-shadow: 0 2px 12px rgba(0, 0, 0, 0.08);
  margin-bottom: 24px;
  padding: 20px 24px;
  display: flex;
  justify-content: space-between;
  align-items: center;

  .header-title {
    display: flex;
    align-items: center;
    // justify-content: space-around;
    width: 100%;
    text-align: center;    justify-content: center;
    gap: 8px;
    
    i {
      font-size: 20px;
      color: #409EFF;
    }

    span {
      font-size: 18px;
      font-weight: 600;
      color: #303133;
    }
  }
}

.search-card {
  margin-bottom: 24px;

  .search-form {
    display: flex;
    justify-content: center;
    align-items: center;
    gap: 16px;

    .el-form-item {
      margin-bottom: 0;
      margin-right: 0;
    }

    .el-input {
      width: 300px;
    }
  }
}

.table-card {
  .cell-text {
    color: #606266;
  }

  .el-table {
    margin: 0 auto;
  }
}

.pagination-container {
  padding: 20px 0;
  display: flex;
  justify-content: center;
}

::v-deep .el-table {
  th {
    background-color: #f5f7fa;
    color: #303133;
    font-weight: 600;
  }

  .el-button + .el-button {
    margin-left: 8px;
  }
}
</style> 